package com.super8.super8newtv.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.super8.super8newtv.R;
import com.super8.super8newtv.bean.HotelpriceBean;

import java.util.List;

public class HotelPriceAdapter extends XRecyclerView.Adapter<HotelPriceAdapter.HotelPriceViewHolder> {
    private Context context;
    private List<HotelpriceBean> dataBeans;
    HotelpriceBean bean;
    int dataNum = 1;
    /**
     * 添加数据
     */
    public void setDataNum(int dataNum) {
        this.dataNum = dataNum;
    }




    public HotelPriceAdapter(Context context, List<HotelpriceBean> dataBeans) {
        this.context = context;
        this.dataBeans = dataBeans;
    }

    @Override
    public int getItemCount() {
        // 返回item数量
        return dataBeans.isEmpty() ? 0 : (dataBeans.size() * dataNum);
    }

    @NonNull
    @Override
    public HotelPriceAdapter.HotelPriceViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.hotel_price_item, viewGroup, false);
        return new HotelPriceViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull HotelPriceAdapter.HotelPriceViewHolder viewHolder, int position) {
        bean = null;
        if (!dataBeans.isEmpty()) {
            bean = dataBeans.get(position % dataBeans.size());
        }
        if (bean != null) {
            viewHolder.tv_hotel_room.setText(bean.getRoomTypeName());
            viewHolder.tv_door_price.setText(bean.getCounterPrice());
            viewHolder.tv_guest_price.setText(bean.getVIPPrice());
            viewHolder.tv_gold_price.setText(bean.getGoldPrice());
            viewHolder.tv_super_price.setText(bean.getSuperPrice());
        }
        if (position % 2 != 1) {
            viewHolder.lin_item_parent.setBackgroundResource(R.mipmap.odd_bg);
        } else {
            viewHolder.lin_item_parent.setBackgroundResource(0);
        }
        if (position == (dataBeans.size() * dataNum) - 1) {
            this.onLastItemLister.onLastItem();
        }
    }

    class HotelPriceViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_hotel_room;
        private TextView tv_door_price;
        private TextView tv_guest_price;
        private TextView tv_gold_price;
        private TextView tv_super_price;
        private LinearLayout lin_item_parent;

        HotelPriceViewHolder(View view) {
            super(view);
            tv_hotel_room = view.findViewById(R.id.tv_hotel_room);
            tv_door_price = view.findViewById(R.id.tv_door_price);
            tv_guest_price = view.findViewById(R.id.tv_guest_price);
            tv_gold_price = view.findViewById(R.id.tv_gold_price);
            tv_super_price = view.findViewById(R.id.tv_super_price);
            lin_item_parent = view.findViewById(R.id.lin_item_parent);
        }
    }

    private OnLastItemLister onLastItemLister;

    public void setOnItemClickListener(OnLastItemLister listener) {
        this.onLastItemLister = listener;
    }

    public interface OnLastItemLister {
        void onLastItem();
    }

}
